package edu.hubu.shop9.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hubu.shop9.common.JsonResult;
import edu.hubu.shop9.entity.Department;
import edu.hubu.shop9.query.DepartmentQuery;
import edu.hubu.shop9.query.EmployeeQuery;
import edu.hubu.shop9.service.IDepartmentService;
import edu.hubu.shop9.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author waxijiang
 * @since 2021-07-26
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    @RequestMapping("/departmentpage")
    public String departmentpage(Model model){
        List<Department> list = departmentService.list();
        model.addAttribute("list", list);
        return "department/departmentList";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Department> selectAll() {
        List<Department> list = departmentService.list();
        return list;
    }


    @RequestMapping("/page")
    @ResponseBody
    public Map<String, Object> selectAll(DepartmentQuery query, int page, int rows) {
        Page<Department> page1 = new Page<>(page, rows);

        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        Map<String, Object> fields = ObjectUtils.getNotNullFields(query);
        // 构造like条件
        for (Map.Entry<String, Object> entry : fields.entrySet()) {
            if (!"".equals(entry.getValue())) {
                if (entry.getValue() instanceof String) {
                    wrapper.like(entry.getKey(), entry.getValue());
                } else if (entry.getValue() instanceof Integer) {
                    wrapper.eq(entry.getKey(), entry.getValue());
                }
            }
        }

        IPage<Department> pageInfo = departmentService.selectByQuery(wrapper, page1);
        // 使用map传递到前台
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("rows", pageInfo.getRecords());
        return resultMap;
    }


    @RequestMapping("/deleteByKey")
    @ResponseBody
    public JsonResult deleteByKey(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            departmentService.removeById(id);
            jsonResult.setMsg("已经删掉了");
        } catch (Exception e) {
            e.printStackTrace();
        }
//		使用map传递到前台
        return jsonResult;
    }


    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Department department) {
        JsonResult jsonResult = new JsonResult();
        try {
            departmentService.save(department);
            jsonResult.setMsg("添加成功");
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(Department department) {
        JsonResult jsonResult = new JsonResult();
        try {
            departmentService.updateById(department);
            jsonResult.setMsg("修改成功");
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

}

